package com.wing.bill.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.bill.dao.BillOrdInformationDao;
import com.wing.bill.model.entity.BillOrdInformation;
import com.wing.bill.model.request.BillOrdInformationForm;
import com.wing.bill.model.response.BillOrdInformationVO;
import com.wing.util.PulsarCommons;
import com.wing.util.PulsarUtils;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账单表账单汇总-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class BillOrdInformationServiceImpl extends ServiceImpl<BillOrdInformationDao, BillOrdInformation> implements BillOrdInformationService {
    @Value("${pulsar.client.topicUrl}")
    private String topicUrl;

    @Override
    public int add(BillOrdInformationForm billOrdInformationForm) {

        //校验

        BillOrdInformation billOrdInformation =new BillOrdInformation();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(BillOrdInformation.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(billOrdInformationForm, billOrdInformation, copyOptions);

        return baseMapper.insert(billOrdInformation);
    }

    @Override
    public List<BillOrdInformationVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<BillOrdInformationVO> pageList4app(Page page, Map<String, Object> params) {
        List<BillOrdInformationVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public BillOrdInformationVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public int confirmBill(BillOrdInformationForm billOrdInformationForm) {
        BillOrdInformation boif = new BillOrdInformation();
        boif.setId(billOrdInformationForm.getId());
        if(billOrdInformationForm.getConfirmResult().equals(BillOrdInformation.ConfirmResult.refuse)){
            boif.setConfirmRefuseRemark(billOrdInformationForm.getConfirmRefuseRemark());//企业确认不通过 不通过的理由  平台重新审核
            boif.setBillStatus(BillOrdInformation.BillStatus.pending);//企业确认不通过 状态到待审核
            boif.setConfirmResult(BillOrdInformation.ConfirmResult.refuse);
        }else {
            boif.setBillStatus(BillOrdInformation.BillStatus.insettle)
                    .setConfirmResult(BillOrdInformation.ConfirmResult.pass);//企业确认通过 账单到结算中
        }
        boif.setConfirmResult(billOrdInformationForm.getConfirmResult())
                .setConfirmName(JwtUtils.getLoginUsername())
                .setConfirmRemark(billOrdInformationForm.getConfirmRemark())
                .setConfirmTime(billOrdInformationForm.getConfirmTime());
        //todo 同步到平台
        try{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", "Update");
            jsonObject.put("data", boif);
            PulsarUtils.send(topicUrl+ PulsarCommons.BILL_ORD_INFORMATION_TOPIC,jsonObject.toJSONString());
        }catch (Exception e) {
            log.error(PulsarCommons.BILL_ORD_INFORMATION_TOPIC+"同步失败",e);
        }
        return baseMapper.updateById(boif);
    }

    @Override
    public HashMap queryBillCensus(LocalDateTime last, LocalDateTime now, String type) {
        return baseMapper.queryBillCensus(last,now,type);
    }

    @Override
    public HashMap queryBillCensusDetail(String billNo) {
        return baseMapper.queryBillCensusDetail(billNo);
    }

    @Override
    public HashMap find(String billNo) {
        return baseMapper.find(billNo);
    }
}
